﻿using LbangCMS.Areas.Admin.Models;
using LbangCMS.EFModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LbangCMS.Extensions;

namespace LbangCMS.Areas.Admin.Controllers
{
    [Authorize]
    public class VideoCategoryController : Controller
    {
        // GET: Admin/ContentCategory
        public ActionResult List()
        {
            var model = new List<CategoryModel>();
            var categories = GetCategoryList();
            if(categories!=null&&categories.Any())
            {
                foreach (var item in categories)
                {
                    model.Add(new CategoryModel { ID =item.ID,Name=item.Name,SortIndex=item.SortIndex });
                }
            }
            if(Request.IsAjaxRequest())
            {
                return PartialView("_ListPartial", model);
            }
            return View(model);
        }

        public ActionResult Edit(int id=0)
        {
            if(id>0)
            {
                var category = GetCategory(id);
                if(category!=null)
                {
                    var model = new CategoryModel()
                    {
                        ID = category.ID,
                        Name = category.Name,
                        SortIndex = category.SortIndex
                    };
                    return View(model);
                }
            }
            return View();
        }

        [HttpPost]
        public ActionResult Edit(CategoryModel model)
        {
            if(ModelState.IsValid)
            {
                if(model.ID>0)
                {
                    UpdateCategory(model.ID, model.Name, model.SortIndex);
                }
                else
                {
                    AddCategory(model.Name, model.SortIndex);
                }
                return Json(new { success = true });
            }
            else
            {
                return Json(new { success = false,errorMsg=ModelState.ToHtmlMessage() });
            }
        }

        public ActionResult Remove(int id)
        {
            RemoveCategory(id);
            return Json(new { success = true });
        }

        private VideoCategory GetCategory(int id)
        {
            using (var db = new Database())
            {
                return  db.VideoCategories.Where(o => o.ID == id).FirstOrDefault();
            }
        }

        private void AddCategory(string name,int sortIndex)
        {
            using (var db = new Database())
            {
                var category = new VideoCategory();
                category.Name = name;
                category.SortIndex = sortIndex;
                db.VideoCategories.Add(category);
                db.SaveChanges();
            }
        }
        private void UpdateCategory(int id,string name,int sortIndex)
        {
            using (var db = new Database())
            {
                var category = db.VideoCategories.Where(o => o.ID == id).FirstOrDefault();
                if(category!=null)
                {
                    category.Name = name;
                    category.SortIndex = sortIndex;
                    db.SaveChanges();
                }
            }
        }
        private void RemoveCategory(int id)
        {
            using (var db = new Database())
            {
                var category = db.VideoCategories.Where(o => o.ID == id).FirstOrDefault();
                if (category != null)
                {
                    db.VideoCategories.Remove(category);
                    db.SaveChanges();
                }
            }
        }

        public List<VideoCategory> GetCategoryList()
        {
            using (var db = new Database())
            {
                return db.VideoCategories.OrderBy(o => o.SortIndex).ToList();
            }
        }




    }
}